<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      10-Docker之Docker-Compose | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>10-Docker之Docker-Compose</h2>



  <p class="post-date">2019-03-28</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h2 id="一-Docker-Compose-简介"><a href="#一-Docker-Compose-简介" class="headerlink" title="一 Docker Compose 简介"></a>一 Docker Compose 简介</h2><h3 id="1-1-Docker-Compose介绍"><a href="#1-1-Docker-Compose介绍" class="headerlink" title="1.1 Docker Compose介绍"></a>1.1 Docker Compose介绍</h3><blockquote>
<p>•Docker Compose是一个能一次性定义和管理多个Docker容器的工具。</p>
<p>•详细地说：</p>
<p>​     Compose中定义和启动的每一个容器都相当于一个服务(service)</p>
<p>​     Compose中能定义和启动多个服务，且它们之间通常具有协同关系</p>
<p>•管理方式：</p>
<p>​     使用YAML文件来配置我们应用程序的服务。</p>
<pre><code>使用单个命令（docker-compose up），就可以创建并启动配置文件中配置的所有服务。</code></pre></blockquote>
<h3 id="1-2-Docker-Compose-工作原理"><a href="#1-2-Docker-Compose-工作原理" class="headerlink" title="1.2 Docker Compose 工作原理"></a>1.2 Docker Compose 工作原理</h3><p><img src="https://tva1.sinaimg.cn/large/00831rSTly1gdcgdd0n4pj30mo0fuzmy.jpg" alt="image-20200331011006843"></p>
<h3 id="1-3-Docker-Compose安装"><a href="#1-3-Docker-Compose安装" class="headerlink" title="1.3 Docker Compose安装"></a>1.3 Docker Compose安装</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Docker for Mac与Docker for Windows自带docker-compose</span></span><br><span class="line"><span class="comment"># Linux下需要单独安装：</span></span><br><span class="line"><span class="comment"># 第一步： sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose</span></span><br><span class="line"><span class="comment"># 第二步：sudo chmod +x /usr/local/bin/docker-compose</span></span><br><span class="line"><span class="comment"># 终端中使用docker-compose --version查看安装的版本</span></span><br><span class="line">这里示例安装版本是<span class="number">1.21</span><span class="number">.2</span>，很可能您看到这里时，已经出现更新的版本，因此建议换成最新版本。</span><br><span class="line"><span class="comment"># 访问github可能网络原因下载失败，可以使用国内镜像</span></span><br><span class="line">curl -L https://get.daocloud.io/docker/compose/releases/download/<span class="number">1.25</span><span class="number">.4</span>/docker-compose-`uname -s`-`uname -m` &gt; /usr/local/bin/docker-compose</span><br><span class="line">chmod +x /usr/local/bin/docker-compose</span><br></pre></td></tr></table></figure>

<h3 id="1-4-Docker-Compose-CLI"><a href="#1-4-Docker-Compose-CLI" class="headerlink" title="1.4 Docker Compose CLI"></a>1.4 Docker Compose CLI</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">利用docker-compose --help查看</span><br><span class="line">对比后会发现：Docker Compose CLI的很多命令的功能和Docker Client CLI是相似的。最主要的区别就是前者能一次性运行管理多个容器，后者只能一次管理一个。</span><br></pre></td></tr></table></figure>

<h2 id="二-了解-Docker-Compose-File"><a href="#二-了解-Docker-Compose-File" class="headerlink" title="二 了解 Docker Compose File"></a>二 了解 Docker Compose File</h2><h3 id="2-1Docker-Compose-File版本"><a href="#2-1Docker-Compose-File版本" class="headerlink" title="2.1Docker Compose File版本"></a>2.1Docker Compose File版本</h3><p>•<a href="https://docs.docker.com/compose/compose-file/" target="_blank" rel="noopener">Docker Compose File</a> 有多个版本，基本是向后兼容的，但也有极个别配置项高版本中没有。</p>
<p>•在docker-compose.yml一开始就需要利用version关键词标明当前file使用的版本</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gge280kritj30y80u0mzk.jpg" alt="image-20200703203240766"></p>
<h3 id="2-2-Docker-Compose-File-TOP配置参数概览"><a href="#2-2-Docker-Compose-File-TOP配置参数概览" class="headerlink" title="2.2 Docker Compose File TOP配置参数概览"></a>2.2 Docker Compose File TOP配置参数概览</h3><blockquote>
<p>Docker Compose File 顶级配置项：</p>
<p>​    •version：指定Docker Compose File版本号</p>
<p>​    •services：定义多个服务并配置启动参数</p>
<p>​    •volumes：声明或创建在多个服务中共同使用的数据卷对象</p>
<p>​    •networks：定义在多个服务中共同使用的网络对象</p>
<p>​    •configs：声明将在本服务中要使用的一些配置文件</p>
<p>​    •secrets：声明将在本服务中要使用的一些秘钥、密码文件</p>
<p>​    •x-***：自定义配置。主要用于复用相同的配置。</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># version：有1，2，3版本，目前都用"3"</span></span><br><span class="line"> <span class="comment"># 一个service代表一个container，这个container可以从dockerhub的image来创建，或者从本地dockerfilebuild的image来创建</span></span><br><span class="line"> <span class="comment"># service的启动类似docker run，可以指定network和volume，所有可以给servier指定network和volume</span></span><br></pre></td></tr></table></figure>

<h3 id="2-3-docker-compose命令"><a href="#2-3-docker-compose命令" class="headerlink" title="2.3 docker-compose命令"></a>2.3 docker-compose命令</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 启动管理容器</span></span><br><span class="line">docker-compose up  <span class="comment"># 会自动搜索当前路径下的 docker-compose.yml文件</span></span><br><span class="line">docker-compose -f 指定文件 up</span><br><span class="line">docker-compose up -d  <span class="comment"># 后台执行，一般我们看日志输出，不用这个</span></span><br><span class="line"></span><br><span class="line">docker-compose stop  <span class="comment"># 停止，不会删除容器和镜像</span></span><br><span class="line">docker-compose down <span class="comment"># 停止，并删除关联的容器</span></span><br><span class="line">docker-compose start  <span class="comment"># 启动yml文件管理的容器</span></span><br><span class="line">docker-compose ps    <span class="comment"># 正在运行的容器</span></span><br><span class="line">docker-compose images <span class="comment"># docker-compose管理的容器</span></span><br><span class="line"></span><br><span class="line">docker-compose <span class="keyword">exec</span> yml文件中写的service /bin/bash  <span class="comment"># 进入到容器内</span></span><br></pre></td></tr></table></figure>



<img src="https://tva1.sinaimg.cn/large/00831rSTly1gdcgkfk88xj30gg0imn08.jpg" alt="image-20200331011654608" style="zoom:67%;" />

<h3 id="2-3-docker-compose案例"><a href="#2-3-docker-compose案例" class="headerlink" title="2.3 docker-compose案例"></a>2.3 docker-compose案例</h3><h4 id="案例一"><a href="#案例一" class="headerlink" title="案例一"></a>案例一</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">services:</span><br><span class="line">  db:</span><br><span class="line">    image: mysql:<span class="number">5.7</span></span><br><span class="line">    volumes:</span><br><span class="line">      - <span class="string">"db-data:/var/lib/mysql"</span></span><br><span class="line">    networks:</span><br><span class="line">      - my-bridge</span><br><span class="line"><span class="comment"># 等同于</span></span><br><span class="line"><span class="comment"># docker run -d --network my-bridge -v db-data"/var/lib/mysql mysql:5.7</span></span><br></pre></td></tr></table></figure>



<h2 id="三-Docker-Compose-部署应用，水平扩展"><a href="#三-Docker-Compose-部署应用，水平扩展" class="headerlink" title="三 Docker Compose 部署应用，水平扩展"></a>三 Docker Compose 部署应用，水平扩展</h2><h3 id="3-1-使用Docker-Compose部署一个wordpress"><a href="#3-1-使用Docker-Compose部署一个wordpress" class="headerlink" title="3.1 使用Docker Compose部署一个wordpress"></a>3.1 使用Docker Compose部署一个wordpress</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 第一步：docker-compose.yml</span></span><br><span class="line">version: <span class="string">'3'</span></span><br><span class="line">services:</span><br><span class="line">  wordpress:</span><br><span class="line">    image: wordpress</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">8080</span>:<span class="number">80</span></span><br><span class="line">    depends_on:</span><br><span class="line">      - mysql</span><br><span class="line">    environment:</span><br><span class="line">      WORDPRESS_DB_HOST: mysql</span><br><span class="line">      WORDPRESS_DB_PASSWORD: root</span><br><span class="line">    networks:</span><br><span class="line">      - my-bridge</span><br><span class="line"></span><br><span class="line">  mysql:</span><br><span class="line">    image: mysql:<span class="number">5.7</span></span><br><span class="line">    environment:</span><br><span class="line">      MYSQL_ROOT_PASSWORD: root</span><br><span class="line">      MYSQL_DATABASE: wordpress</span><br><span class="line">    volumes:</span><br><span class="line">      - mysql-data:/var/lib/mysql</span><br><span class="line">    networks:</span><br><span class="line">      - my-bridge</span><br><span class="line"></span><br><span class="line">volumes:</span><br><span class="line">  mysql-data:</span><br><span class="line"></span><br><span class="line">networks:</span><br><span class="line">  my-bridge:</span><br><span class="line">    driver: bridge</span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line"><span class="comment"># 第二步：docker-compose up</span></span><br></pre></td></tr></table></figure>



<h3 id="3-2-Docker-Compose-案例一-小型web服务项目搭建"><a href="#3-2-Docker-Compose-案例一-小型web服务项目搭建" class="headerlink" title="3.2  Docker Compose 案例一  小型web服务项目搭建"></a>3.2  Docker Compose 案例一  小型web服务项目搭建</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 第一步：编写dDockerfile</span></span><br><span class="line">FROM python:<span class="number">3.6</span></span><br><span class="line">COPY . /app</span><br><span class="line">WORKDIR /app</span><br><span class="line">RUN pip install flask redis</span><br><span class="line">EXPOSE <span class="number">5000</span></span><br><span class="line">CMD [ <span class="string">"python"</span>, <span class="string">"app.py"</span> ]</span><br><span class="line"></span><br><span class="line"><span class="comment">#第二步：编写docker-compose.yml</span></span><br><span class="line">version: <span class="string">"3"</span></span><br><span class="line"></span><br><span class="line">services:</span><br><span class="line"></span><br><span class="line">  redis:</span><br><span class="line">    image: redis</span><br><span class="line"></span><br><span class="line">  web:</span><br><span class="line">    build:</span><br><span class="line">      context: .</span><br><span class="line">      dockerfile: Dockerfile</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">8080</span>:<span class="number">5000</span></span><br><span class="line">    environment:</span><br><span class="line">      REDIS_HOST: redis</span><br><span class="line"> <span class="comment"># 第三步：编写app.py</span></span><br><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"><span class="keyword">from</span> redis <span class="keyword">import</span> Redis</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line">redis = Redis(host=os.environ.get(<span class="string">'REDIS_HOST'</span>, <span class="string">'127.0.0.1'</span>), port=<span class="number">6379</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route('/')</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">hello</span><span class="params">()</span>:</span></span><br><span class="line">    redis.incr(<span class="string">'hits'</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">'你好! 查看 %s 次， hostname 是 %s.\n'</span> % (redis.get(<span class="string">'hits'</span>),socket.gethostname())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line">    app.run(host=<span class="string">"0.0.0.0"</span>, port=<span class="number">5000</span>, debug=<span class="literal">True</span>)</span><br><span class="line"><span class="comment"># 第四步：运行</span></span><br><span class="line">docker-compose up</span><br></pre></td></tr></table></figure>

<h3 id="3-3-docker-compose水平扩展"><a href="#3-3-docker-compose水平扩展" class="headerlink" title="3.3 docker-compose水平扩展"></a>3.3 docker-compose水平扩展</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 把flask_redis项目扩展成三个</span></span><br><span class="line">docker-compose up --help</span><br><span class="line"><span class="comment"># scale SERVICE=NUM设置成三个</span></span><br><span class="line">docker-compose up --scale web=<span class="number">3</span>   <span class="comment"># 执行有问题，因为8080端口已经被映射了</span></span><br><span class="line"><span class="comment"># 删除docker-compose.yml中的port</span></span><br><span class="line"><span class="comment"># 在启动</span></span><br><span class="line">docker-compose up --scale web=<span class="number">3</span></span><br><span class="line">docker-compose start</span><br><span class="line">docker-compose ps  <span class="comment"># 可以看到三个web启动了</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 前面加一个负载均衡器HAProxy</span></span><br><span class="line"><span class="comment"># HAProxy和Nginx的区别？万能的百度</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># app.py 改成监听的端口为80</span></span><br><span class="line"><span class="comment"># Dockerfile不需要改</span></span><br><span class="line"><span class="comment"># docker-compose.yml</span></span><br><span class="line">version: <span class="string">"3"</span></span><br><span class="line"></span><br><span class="line">services:</span><br><span class="line"></span><br><span class="line">  redis:</span><br><span class="line">    image: redis</span><br><span class="line"></span><br><span class="line">  web:</span><br><span class="line">    build:</span><br><span class="line">      context: .</span><br><span class="line">      dockerfile: Dockerfile</span><br><span class="line">    environment:</span><br><span class="line">      REDIS_HOST: redis</span><br><span class="line"></span><br><span class="line">  lb:</span><br><span class="line">    image: dockercloud/haproxy</span><br><span class="line">    links:</span><br><span class="line">      - web</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">8080</span>:<span class="number">80</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /var/run/docker.sock:/var/run/docker.sock </span><br><span class="line"><span class="comment">#启动，从浏览器查看</span></span><br><span class="line">docker-compose up</span><br><span class="line"><span class="comment"># 把web扩展为3个从浏览器查看</span></span><br><span class="line">docker-compose up --scale web=<span class="number">3</span> -d</span><br><span class="line"><span class="comment"># 减掉为1个</span></span><br><span class="line">docker-compose up --scale web=<span class="number">1</span> -d</span><br></pre></td></tr></table></figure>

<h3 id="3-4-docker-compose部署一个复杂的应用"><a href="#3-4-docker-compose部署一个复杂的应用" class="headerlink" title="3.4 docker-compose部署一个复杂的应用"></a>3.4 docker-compose部署一个复杂的应用</h3><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gg81n0s2c6j31m90u0k8i.jpg" alt="image-20200628153906975" style="zoom:50%;" />

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 项目地址：https://pan.baidu.com/s/1lzfaza5kO-jbRLsEFrslIg  提取码: 加群获 830644110</span></span><br><span class="line"><span class="comment"># 第一步，下载项目，解压，cd进入（内部自带项目，Dockerfile，docker-compose.yml）</span></span><br><span class="line"><span class="comment"># docker-compose.yml如下</span></span><br><span class="line">services:</span><br><span class="line">  voting-app:</span><br><span class="line">    build: ./voting-app/.</span><br><span class="line">    volumes:</span><br><span class="line">     - ./voting-app:/app</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">"5000:80"</span></span><br><span class="line">    links:</span><br><span class="line">      - redis</span><br><span class="line">    networks:</span><br><span class="line">      - front-tier</span><br><span class="line">      - back-tier</span><br><span class="line"></span><br><span class="line">  result-app:</span><br><span class="line">    build: ./result-app/.</span><br><span class="line">    volumes:</span><br><span class="line">      - ./result-app:/app</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">"5001:80"</span></span><br><span class="line">    links:</span><br><span class="line">      - db</span><br><span class="line">    networks:</span><br><span class="line">      - front-tier</span><br><span class="line">      - back-tier</span><br><span class="line"></span><br><span class="line">  worker:</span><br><span class="line">    build: ./worker</span><br><span class="line">    links:</span><br><span class="line">      - db</span><br><span class="line">      - redis</span><br><span class="line">    networks:</span><br><span class="line">      - back-tier</span><br><span class="line"></span><br><span class="line">  redis:</span><br><span class="line">    image: redis</span><br><span class="line">    ports: [<span class="string">"6379"</span>]</span><br><span class="line">    networks:</span><br><span class="line">      - back-tier</span><br><span class="line"></span><br><span class="line">  db:</span><br><span class="line">    image: postgres:<span class="number">9.4</span></span><br><span class="line">    volumes:</span><br><span class="line">      - <span class="string">"db-data:/var/lib/postgresql/data"</span></span><br><span class="line">    networks:</span><br><span class="line">      - back-tier</span><br><span class="line"></span><br><span class="line">volumes:</span><br><span class="line">  db-data:</span><br><span class="line"></span><br><span class="line">networks:</span><br><span class="line">  front-tier:</span><br><span class="line">  back-tier:</span><br><span class="line"><span class="comment"># 第二步：部署</span></span><br><span class="line">docker-compose up  <span class="comment"># 时间很久</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 补充：9a37</span></span><br><span class="line">docker-compose build <span class="comment"># 如果yml文件中有需要先build的image，会先构建，然后再up，节约时间（并不会节约）</span></span><br></pre></td></tr></table></figure>



<h3 id="3-5-docker-compose单机环境ELK系统"><a href="#3-5-docker-compose单机环境ELK系统" class="headerlink" title="3.5 docker-compose单机环境ELK系统"></a>3.5 docker-compose单机环境ELK系统</h3><img src="https://tva1.sinaimg.cn/large/00831rSTly1gdcgmwo1pfj30q20cwdh1.jpg" alt="image-20200331011825994" style="zoom:67%;" />

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 第一步：编写docker-compose.yml（ELK镜像地址:https://www.docker.elastic.co/）</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 第二步：运行</span></span><br><span class="line">docker-compose up</span><br></pre></td></tr></table></figure>

<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 官方提供的</span></span><br><span class="line"><span class="attr">version:</span> <span class="string">'2.2'</span></span><br><span class="line"><span class="attr">services:</span></span><br><span class="line">  <span class="attr">es01:</span></span><br><span class="line">    <span class="attr">image:</span> <span class="string">docker.elastic.co/elasticsearch/elasticsearch:7.5.2</span></span><br><span class="line">    <span class="attr">container_name:</span> <span class="string">es01</span></span><br><span class="line">    <span class="attr">environment:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">node.name=es01</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">cluster.name=es-docker-cluster</span> <span class="comment"># 集群名称</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">discovery.seed_hosts=es02,es03</span> <span class="comment"># 指定集群</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">cluster.initial_master_nodes=es01,es02,es03</span> <span class="comment"># 设置集群发现</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">bootstrap.memory_lock=true</span> <span class="comment"># 存锁，限制es不会无限制使用内存</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">"ES_JAVA_OPTS=-Xms512m -Xmx512m"</span> <span class="comment"># jvm内存使用</span></span><br><span class="line">    <span class="attr">ulimits:</span> <span class="comment"># 跟上方bootstrap.memory_lock关联</span></span><br><span class="line">      <span class="attr">memlock:</span></span><br><span class="line">        <span class="attr">soft:</span> <span class="number">-1</span></span><br><span class="line">        <span class="attr">hard:</span> <span class="number">-1</span></span><br><span class="line">    <span class="attr">volumes:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">data01:/usr/share/elasticsearch/data</span></span><br><span class="line">    <span class="attr">ports:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="number">9200</span><span class="string">:9200</span></span><br><span class="line">    <span class="attr">networks:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">elastic</span></span><br><span class="line">  <span class="attr">es02:</span></span><br><span class="line">    <span class="attr">image:</span> <span class="string">docker.elastic.co/elasticsearch/elasticsearch:7.5.2</span></span><br><span class="line">    <span class="attr">container_name:</span> <span class="string">es02</span></span><br><span class="line">    <span class="attr">environment:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">node.name=es02</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">cluster.name=es-docker-cluster</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">discovery.seed_hosts=es01,es03</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">cluster.initial_master_nodes=es01,es02,es03</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">bootstrap.memory_lock=true</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">"ES_JAVA_OPTS=-Xms512m -Xmx512m"</span></span><br><span class="line">    <span class="attr">ulimits:</span></span><br><span class="line">      <span class="attr">memlock:</span></span><br><span class="line">        <span class="attr">soft:</span> <span class="number">-1</span></span><br><span class="line">        <span class="attr">hard:</span> <span class="number">-1</span></span><br><span class="line">    <span class="attr">volumes:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">data02:/usr/share/elasticsearch/data</span></span><br><span class="line">    <span class="attr">networks:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">elastic</span></span><br><span class="line">  <span class="attr">es03:</span></span><br><span class="line">    <span class="attr">image:</span> <span class="string">docker.elastic.co/elasticsearch/elasticsearch:7.5.2</span></span><br><span class="line">    <span class="attr">container_name:</span> <span class="string">es03</span></span><br><span class="line">    <span class="attr">environment:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">node.name=es03</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">cluster.name=es-docker-cluster</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">discovery.seed_hosts=es01,es02</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">cluster.initial_master_nodes=es01,es02,es03</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">bootstrap.memory_lock=true</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">"ES_JAVA_OPTS=-Xms512m -Xmx512m"</span></span><br><span class="line">    <span class="attr">ulimits:</span></span><br><span class="line">      <span class="attr">memlock:</span></span><br><span class="line">        <span class="attr">soft:</span> <span class="number">-1</span></span><br><span class="line">        <span class="attr">hard:</span> <span class="number">-1</span></span><br><span class="line">    <span class="attr">volumes:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">data03:/usr/share/elasticsearch/data</span></span><br><span class="line">    <span class="attr">networks:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">elastic</span></span><br><span class="line"></span><br><span class="line"><span class="attr">volumes:</span></span><br><span class="line">  <span class="attr">data01:</span></span><br><span class="line">    <span class="attr">driver:</span> <span class="string">local</span></span><br><span class="line">  <span class="attr">data02:</span></span><br><span class="line">    <span class="attr">driver:</span> <span class="string">local</span></span><br><span class="line">  <span class="attr">data03:</span></span><br><span class="line">    <span class="attr">driver:</span> <span class="string">local</span></span><br><span class="line"></span><br><span class="line"><span class="attr">networks:</span></span><br><span class="line">  <span class="attr">elastic:</span></span><br><span class="line">    <span class="attr">driver:</span> <span class="string">bridge</span></span><br></pre></td></tr></table></figure>

</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#Linux" >
    <span class="tag-code">Linux</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/linux/Docker/8-Docker%E4%B9%8B%E4%BB%93%E5%BA%93/">
        <span class="nav-arrow">← </span>
        
          8-Docker之仓库
        
      </a>
    
    
      <a class="nav-right" href="/linux/Docker/2-Docker%E4%B9%8B%E5%AE%89%E8%A3%85/">
        
          2-Docker之安装
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#一-Docker-Compose-简介"><span class="toc-nav-text">一 Docker Compose 简介</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-1-Docker-Compose介绍"><span class="toc-nav-text">1.1 Docker Compose介绍</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-2-Docker-Compose-工作原理"><span class="toc-nav-text">1.2 Docker Compose 工作原理</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-3-Docker-Compose安装"><span class="toc-nav-text">1.3 Docker Compose安装</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-4-Docker-Compose-CLI"><span class="toc-nav-text">1.4 Docker Compose CLI</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#二-了解-Docker-Compose-File"><span class="toc-nav-text">二 了解 Docker Compose File</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-1Docker-Compose-File版本"><span class="toc-nav-text">2.1Docker Compose File版本</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-2-Docker-Compose-File-TOP配置参数概览"><span class="toc-nav-text">2.2 Docker Compose File TOP配置参数概览</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-3-docker-compose命令"><span class="toc-nav-text">2.3 docker-compose命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-3-docker-compose案例"><span class="toc-nav-text">2.3 docker-compose案例</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#案例一"><span class="toc-nav-text">案例一</span></a></li></ol></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#三-Docker-Compose-部署应用，水平扩展"><span class="toc-nav-text">三 Docker Compose 部署应用，水平扩展</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-1-使用Docker-Compose部署一个wordpress"><span class="toc-nav-text">3.1 使用Docker Compose部署一个wordpress</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-2-Docker-Compose-案例一-小型web服务项目搭建"><span class="toc-nav-text">3.2  Docker Compose 案例一  小型web服务项目搭建</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-3-docker-compose水平扩展"><span class="toc-nav-text">3.3 docker-compose水平扩展</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-4-docker-compose部署一个复杂的应用"><span class="toc-nav-text">3.4 docker-compose部署一个复杂的应用</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-5-docker-compose单机环境ELK系统"><span class="toc-nav-text">3.5 docker-compose单机环境ELK系统</span></a></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/linux/Docker/10-Docker之Docker-Compose/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "10-Docker之Docker-Compose",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>